using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestCecile
{
    public partial class UpdateCreateForm : Form
    {
        private int klantNr = 1;
        private int aantalNieuw = 0;
        private KlantenBrowser klantBrowser = null;

        private int vKlantNr = 1;
        private string vKlnm = "";
        private string vKlvnm = "";
        private string vStraat = "";
        private string vHuisnr = "";
        private string vStad = "";
        private string vPostnr = "";
        private string vTel = "";
        private string vEmail = "";
        private string vOpmerking = "";
        private string vKlType = "";
        
        public UpdateCreateForm()
        {
            InitializeComponent();
        }

        public UpdateCreateForm(int klantNr, DataSet1 dataSet1, KlantenBrowser klBrowser)
        {
            InitializeComponent();
            this.klantNr = klantNr;
            this.dataSet1 = dataSet1;
            this.kLANTBindingSource.DataMember = "KLANT";
            this.kLANTBindingSource.DataSource = this.dataSet1;
            this.klantBrowser = klBrowser;
        }

      
        private void UpdateCreateForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dataSet1.KLANT' table. You can move, or remove it, as needed.
            //this.kLANTTableAdapter.Fill(this.dataSet1.KLANT);
            //this.dataSet1 = ds;
            aantalNieuw = 0;
            if (klantNr == -99)
            {
                Text = "NIEUWE KLANT";
                kLANTBindingSource.AddNew();
            }
            else
            {
                Text = "WIJZIG KLANT";
                btnNieuw.Visible = false;
                int volgnr = kLANTBindingSource.Find("KLNR", klantNr);
                kLANTBindingSource.Position = volgnr;
                DataRowView drv = kLANTBindingSource.Current as DataRowView;
                string vKlType = drv["KLTYPE"].ToString();
                if (!vKlType.Equals("X"))
                    chkKorting.Checked = true;
            }
        }

       
        private void maakVariabelen()
        {
            vKlnm = txtKlnm.Text.Trim();
            if (vKlnm.Length > 0 && vKlnm.Substring(0, 1).Equals("'")) 
                vKlnm = "'" + vKlnm;
            vKlvnm = txtKlvnm.Text.Trim();
            if (vKlvnm.Length > 0 && vKlvnm.Substring(0, 1).Equals("'"))
                vKlvnm = "'" + vKlvnm;
            vStraat = txtStraat.Text.Trim();
            if (vStraat.Length > 0 && vStraat.Substring(0, 1).Equals("'"))
                vStraat = "'" + vStraat;
            vHuisnr = txtHuisnr.Text.Trim();
            if (vHuisnr.Length > 0 && vHuisnr.Substring(0, 1).Equals("'"))
                vHuisnr = "'" + vHuisnr;
            vStad = txtStad.Text.Trim();
            if (vStad.Length > 0 && vStad.Substring(0, 1).Equals("'"))
                vStad = "'" + vStad;
            vPostnr = txtPostnr.Text.Trim();
            if (vPostnr.Length > 0 && vPostnr.Substring(0, 1).Equals("'"))
                vPostnr = "'" + vPostnr;
            vTel = txtTel.Text.Trim();
            if (vTel.Length > 0 && vTel.Substring(0, 1).Equals("'"))
                vTel = "'" + vTel;
            vEmail = txtEmail.Text.Trim();
            if (vEmail.Length > 0 && vEmail.Substring(0, 1).Equals("'"))
                vEmail = "'" + vEmail;
            vOpmerking = txtOpmerking.Text.Trim();
            if (vOpmerking.Length > 0 && vOpmerking.Substring(0, 1).Equals("'"))
                vOpmerking = "'" + vOpmerking;
            vKlType = (chkKorting.Checked?"Q":"X");
        }

        private void registreerNieuweKlant()
        {
            vKlantNr = int.Parse(kLANTTableAdapter.getMaxKlnr().ToString())+1;
            maakVariabelen();
            System.Data.Odbc.OdbcConnection connect;
            System.Data.Odbc.OdbcCommand cmd;
            connect = new System.Data.Odbc.OdbcConnection();
            //connect.ConnectionString = "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=" + filePath + ";";
            connect.ConnectionString = Properties.Settings.Default.ConnectionString;
            string cmdString = "INSERT INTO KLANT "+
                               "(KLNR, KLNM, KLVNM, KLTYPE, STRAAT, HUISNR, POSTNR, STAD, TEL1, EMAIL, AKORTING, OPMERKING) " +
            "VALUES (" +
            vKlantNr + ", " +
            "'" + vKlnm + "'" + ", " +
            "'" + vKlvnm + "'" + ", " +
            "'" + vKlType + "'" + ", " +
            "'" + vStraat + "'" + ", " +
            "'" + vHuisnr + "'" + ", " +
            "'" + vPostnr + "'" + ", " +
            "'" + vStad + "'" + ", " +
            "'" + vTel + "'" + ", " +
            "'" + vEmail + "'" + ", " +
            0+", "+
            "'" + vOpmerking + "'" + ")";
            
            //MessageBox.Show(cmdString);
            cmd = new System.Data.Odbc.OdbcCommand(cmdString, connect);
            //Text = cmdString;
            try
            {
                connect.Open();
                cmd.ExecuteNonQuery();
                aantalNieuw++;
            }
            finally
            {
                connect.Close();
            }
        }

        private void registreerUpdateKlant()
        {
            try
            {
                vKlantNr = int.Parse(txtKlnr.Text);
                maakVariabelen();
                //string filePath = @"C:\DBFCECILE";
                System.Data.Odbc.OdbcConnection connect;
                System.Data.Odbc.OdbcCommand cmd;
                connect = new System.Data.Odbc.OdbcConnection();
                //connect.ConnectionString = "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=" + filePath + ";";
                connect.ConnectionString = Properties.Settings.Default.ConnectionString;
                string cmdString = "UPDATE KLANT SET " +
                    "KLNM = " + "'" + vKlnm + "'" + ", " +
                    "KLVNM = " + "'" + vKlvnm + "'" + ", " +
                    "KLTYPE = " + "'" + vKlType + "'" + ", " +
                    "STRAAT = " + "'" + vStraat + "'" + ", " +
                    "HUISNR = " + "'" + vHuisnr + "'" + ", " +
                    "POSTNR = " + "'" + vPostnr + "'" + ", " +
                    "STAD = " + "'" + vStad + "'" + ", " +
                    "TEL1 = " + "'" + vTel + "'" + ", " +
                    "EMAIL = " + "'" + vEmail + "'" + ", " +
                    "OPMERKING = " + "'" + vOpmerking + "'" +
                    " WHERE KLNR = " + vKlantNr;

                //MessageBox.Show(cmdString);
                cmd = new System.Data.Odbc.OdbcCommand(cmdString, connect);
                try
                {
                    connect.Open();
                    cmd.ExecuteNonQuery();
                }
                finally
                {
                    connect.Close();
                }
            }
            catch (Exception ex) { ;}// MessageBox.Show(ex.Message); }
        }

        private void btnRegistreer_Click(object sender, EventArgs e)
        {
            if (klantNr == -99)
            {
                if (txtKlnm.Text.Length > 0)
                {
                    registreerNieuweKlant();
                    Close();
                }
                else
                {
                    MessageBox.Show("KLANTNAAM is niet ingevuld!!!");
                    txtKlnm.Focus();
                }
            }

            else
            {
                registreerUpdateKlant();
                Close();
            }    
        }

        private void btnNieuw_Click(object sender, EventArgs e)
        {
            if (txtKlnm.Text.Length > 0)
            {
                registreerNieuweKlant();
                kLANTBindingSource.AddNew();
                txtKlnm.Focus();
            }
            else
            {
                MessageBox.Show("KLANTNAAM is niet ingevuld!!!");
                txtKlnm.Focus();
            }
        }

        private void btnSluit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void UpdateCreateForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (aantalNieuw > 0)
            {
                this.kLANTTableAdapter.FillZonderDeleted(this.dataSet1.KLANT);
                klantBrowser.PositioneerOpNieuweKlant(vKlantNr);
            }
        }

       
    }
}